package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.vo.FinanceProductVo;
import cn.itcast.vo.FinanceVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.annotation.ApplicationScope;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceDao financeDao;

    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ContractProductDao contractProductDao;
    /*
        分页查询
     */
    public PageInfo findAll(int page, int size, FinanceExample example) {
        PageHelper.startPage(page,size);
        //查询所有数据
        List<Finance> list = financeDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public Finance findById(String id) {
        return null;
    }

    @Override
    public void save(Finance finance) {
        //根据集装箱单号查询财务发票
        Invoice invoice = invoiceDao.selectByPrimaryKey(finance.getId());
        //修改财务发票状态
        invoice.setState(2);
        //保存财务发票
        invoiceDao.updateByPrimaryKeySelective(invoice);
        //根据集装箱编号查询装箱数据
        Packing packing = packingDao.selectByPrimaryKey(finance.getId());
        finance.setExportNos(packing.getExportNos());
        //根据合同ID查询委托单数据
        Shipping shipping = shippingDao.selectByPrimaryKey(finance.getId());
        finance.setShipmentPort(shipping.getPortOfLoading());
        finance.setDestinationPort(shipping.getPortOfDischarge());
        //计算出口总金额

        //计算采购总金额

        //计算关税总金额


        //保存财务报运单
        financeDao.insertSelective(finance);
    }

    @Override
    public void update(Finance finance) {

    }

    @Override
    public void delete(String id) {

    }

    //封装报运单表头数据
    public Map<String,String> findFinance(String id) {
        //人家报运单号查询报运单数据
        Finance finance = financeDao.selectByPrimaryKey(id);
        //根据报运合同单号查询报运单合同数据
        //Export export= exportDao.findByCustomerContract(finance.getExportNos());
        //封装数据
        Map<String,String> map=new HashMap<>();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(finance.getInputDate());
        map.put("exportNos",format);
        map.put("customerContract",finance.getExportNos());
       // map.put("consignee",export.getConsignee());
       // map.put("shipmentPort",export.getShipmentPort());
       // map.put("destinationPort",export.getDestinationPort());
       // map.put("priceCondition",export.getPriceCondition());
        return map;
    }

    //查询财务报运单的所有商品数据内容
    public List<FinanceProductVo> findFinancePrdout(String id) {
        //人家报运单号查询报运单数据
        Finance finance = financeDao.selectByPrimaryKey(id);
        //根据报运合同单号查询报运单合同数据
        String[] contractNos = finance.getExportNos().split(" ");
        //根据报运单查询报运商品数据
        List<FinanceProductVo> list = new ArrayList<>();
        for (String contractNo : contractNos) {
            ExportProduct exportProduct = exportProductDao.selectByContractNo(contractNo);
            FinanceProductVo financeProductVo = new FinanceProductVo();
            financeProductVo.setProductNo(exportProduct.getProductNo());
            financeProductVo.setFactoryName(exportProduct.getFactoryName());
            financeProductVo.setUnits(exportProduct.getPackingUnit());
            financeProductVo.setCnumber(exportProduct.getCnumber());
            financeProductVo.setBoxNums(exportProduct.getBoxNum());
            financeProductVo.setGrossWeight(exportProduct.getGrossWeight());
            financeProductVo.setNetWeight(exportProduct.getNetWeight());
            financeProductVo.setSizeLength(exportProduct.getSizeLength());
            financeProductVo.setSizeWidth(exportProduct.getSizeWidth());
            financeProductVo.setSizeHeight(exportProduct.getSizeHeight());
            financeProductVo.setPrice(exportProduct.getPrice());
            financeProductVo.setTax(exportProduct.getTax());
            financeProductVo.setAcquisition(exportProduct.getExPrice());
            financeProductVo.setAcquisitionPrice(exportProduct.getTax());
            financeProductVo.setTaxes(exportProduct.getExPrice());
            list.add(financeProductVo);
        }
        return list;
    }
}
